package com.hrbxlh.util;


import org.apache.commons.lang.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 类说明：手机号、固定电话合法性校验
 */

/**
 * Created by Administrator on 2018/9/10.
 */
public class PhoneValidatorUtil {
    public static boolean matchPhone(String number, int type){
        if (StringUtils.isBlank(number)) {
            return false;
        }
        if (type == 0) {
            Matcher m = null;
            Pattern p = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$");  // 验证带区号的,中间有"-"
            //p = Pattern.compile("^[1-9]{1}[0-9]{5,8}$");         // 验证没有区号的
            m = p.matcher(number);
            return m.matches();
        } else {
            //符合返回true
            if (Pattern.matches("^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9]|17[0|6|7|8])\\d{8}$", number)) {
                return true;
            }
        }
        return false;
    }

    /**
     *
     * @param
     * @param number
     * @param type 0 固话 1 手机号
     * @return
     */
    public static boolean matchPhoneShowToast(String number){
        boolean flagOne = matchPhone(number, 0);
        boolean flagTwo = matchPhone(number,1);
        if(flagOne||flagTwo){
            return true;
        }
        return false;
    }
}
